package com.hx.controller.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * 异常处理
 */
@ControllerAdvice
public class MyAnnoException {
    /**
     * 如果定义了多个异常处理方案
     * 1.根据异常类型精确匹配
     * 2.如果精确匹配不到则选择能够处理该异常的父类
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(IllegalArgumentException.class)
    public Map<String, Object>resolverJson(Exception ex){
        Map<String, Object> map = new HashMap<>();
        map.put("massage",ex.getMessage());
        map.put("code",500);
        return map;
    }

    @ExceptionHandler(RuntimeException.class)
     public ModelAndView resolverCommon(Exception ex){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("error-common");
        mav.addObject("errorMsg",ex.getMessage());
        return mav;
    }

}
